﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Input;
using LockerRack.Drawers;
using Primitive.Mvvm.Framework.Implementations;
using Primitive.Mvvm.Framework.Interfaces;
using UIController.Models;

namespace UIController.ViewModels
{
    public class LeftTreeViewModel : BaseViewModel
    {
        private ToDoModel _model;
        RelayCommand<CategoryHolder> _createCategoryCommand;
        RelayCommand<CategoryHolder> _selectCategoryCommand;
        RelayCommand<CategoryHolder> _deleteCategoryCommand;
        RelayCommand<CategoryHolder> _moveCategoryCommand;

        public LeftTreeViewModel(IModel model, IMediator mediator) : base(mediator)
        {
            _model = model as ToDoModel;
        }
        
        public Drawer SelectedDrawer
        {
            get { return _model.SelectedDrawer; }
            set
            {
                if (_model.SelectedDrawer != value)
                {
                    _model.SelectedDrawer = value;
                    RaisePropertyChanged(this, () => SelectedDrawer);
                }
            }
        }

        public ICommand CreateCategoryCommand
        {
            get
            {
                if (_createCategoryCommand == null)
                {
                    return _createCategoryCommand = new RelayCommand<CategoryHolder>(CreateCategory, CanCreateCategory);
                }
                return _createCategoryCommand;
            }
        }

        public ICommand SelectCategoryCommand
        {
            get
            {
                if (_selectCategoryCommand == null)
                {
                    return _selectCategoryCommand = new RelayCommand<CategoryHolder>(SelectCategory, CanSelectCategory);
                }
                return _selectCategoryCommand;
            }
        }

        private void SelectCategory(CategoryHolder holder)
        {
            var d = holder.Current as Drawer;
            SelectedDrawer = d;
        }

        private bool CanSelectCategory(CategoryHolder holder)
        {
            return holder != null && holder.Current is Drawer;
        }

        public ICommand DeleteCategoryCommand
        {
            get
            {
                if (_deleteCategoryCommand == null)
                {
                    return _createCategoryCommand = new RelayCommand<CategoryHolder>(CreateCategory, CanCreateCategory);
                }
                return _createCategoryCommand;
            }
        }
        
        public IEnumerable<TreeNode> GetTreeElements()
        {
            foreach (var d in _model.Drawers)
            {
                var treeNode = CreateTreeNode(null, d);
                GetChildren(treeNode, d);
                
                yield return treeNode;
            }
        }

        private void CreateCategory(CategoryHolder holder)
        {
            //var d = new Drawer{Name = holder.Name};
            //var parent = holder.Parent as Drawer;
            //var list = parent == null ? _model.Drawers : parent.Drawers;
            //list.Add(d);
            //if (RefreshViewFunc != null)
            //    RefreshViewFunc(null);
        }

        private bool CanCreateCategory(CategoryHolder holder)
        {
            return holder != null && !string.IsNullOrEmpty(holder.Name);
        }

        private static TreeNode CreateTreeNode(TreeNode parent, Drawer d)
        {
            var treeNode = new TreeNode(d.Name) {Tag = d};
            if (parent != null)
                parent.Nodes.Add(treeNode);
            return treeNode;
        }

        private void GetChildren(TreeNode parentNode, Drawer parentDrawer)
        {
            if (parentDrawer.Drawers == null)
                return;

            try
            {
                foreach (var child in parentDrawer.Drawers)
                {
                    var treeNode = CreateTreeNode(parentNode, child);
                    GetChildren(treeNode, child);
                }
            }
            catch
            {
                
            }
        }
    }
}
